package com.mlethe.calendarview

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.mlethe.calendarview.databinding.ActivityNormalBinding
import com.mlethe.library.calendarview.Calendar
import com.mlethe.library.calendarview.CalendarView
import com.mlethe.library.calendarview.CalendarView.OnScrollOutRangeListener
import java.text.SimpleDateFormat
import java.util.*

class NormalActivity : AppCompatActivity() {

    private lateinit var binding: ActivityNormalBinding

    private var mMonthStartDay = 1
    private var mWeekStartDay = 1
    private var mMode = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityNormalBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.tvTitle.text = SimpleDateFormat("yyyy.MM").format(Date())
        binding.btnAdd.setOnClickListener {
            mMonthStartDay++
            if (mMonthStartDay == 32) {
                mMonthStartDay = 1
            }
            binding.calendarView.setMonthStartDay(mMonthStartDay)
        }
        binding.btnReduce.setOnClickListener {
            mMonthStartDay--
            if (mMonthStartDay == 0) {
                mMonthStartDay = 31
            }
            binding.calendarView.setMonthStartDay(mMonthStartDay)
        }
        binding.btnToday.setOnClickListener {
            binding.calendarView.scrollToCurrent(true)
        }

        binding.calendarView.setOnScrollOutRangeListener(object : OnScrollOutRangeListener {
            override fun onScrolledToFirstPage() {
                Toast.makeText(this@NormalActivity, "已滑动到第一页了", Toast.LENGTH_SHORT).show()
            }

            override fun onScrolledToLastPage() {
                Toast.makeText(this@NormalActivity, "已滑动到最后一页了", Toast.LENGTH_SHORT).show()
            }
        })

        binding.calendarView.setOnMonthChangeListener { year, month ->
            Log.d("yym", "onCreate: year->$year, month->$month")
            binding.tvTitle.text = String.format("%d.%02d", year, month)
        }
        binding.calendarView.setOnCalendarSelectListener(object :
            CalendarView.OnCalendarSelectListener {
            override fun onCalendarOutOfRange(calendar: Calendar) {
                Log.e("yym", "onCalendarOutOfRange: calendar->$calendar")
            }

            override fun onCalendarSelect(calendar: Calendar, isClick: Boolean) {
                Log.e(
                    "yym",
                    "onCalendarSelect: calendar->$calendar, week->${calendar.week}, isClick->$isClick"
                )
            }

        })

        binding.btnMonthAdd.setOnClickListener {
            binding.calendarView.scrollToNext()
        }

        binding.btnMonthReduce.setOnClickListener {
            binding.calendarView.scrollToPre()
        }

        binding.btnTime.setOnClickListener {
            binding.calendarView.scrollToCalendar(2022, 12)
        }
        binding.btnRange.setOnClickListener {
            binding.calendarView.setRange(2022, 1, 2023, 2)
        }

        binding.btnWeekAdd.setOnClickListener {
            mWeekStartDay++
            if (mWeekStartDay == 4) {
                mWeekStartDay = 1
            }
            if (mWeekStartDay == 1) {
                binding.calendarView.setWeekStarWithSun()
            } else if (mWeekStartDay == 2) {
                binding.calendarView.setWeekStarWithMon()
            } else if (mWeekStartDay == 3) {
                binding.calendarView.setWeekStarWithSat()
            }
        }
        binding.btnWeekReduce.setOnClickListener {
            mWeekStartDay--
            if (mWeekStartDay == 0) {
                mWeekStartDay = 3
            }
            if (mWeekStartDay == 1) {
                binding.calendarView.setWeekStarWithSun()
            } else if (mWeekStartDay == 2) {
                binding.calendarView.setWeekStarWithMon()
            } else if (mWeekStartDay == 3) {
                binding.calendarView.setWeekStarWithSat()
            }
        }
        binding.btnModeAdd.setOnClickListener {
            mMode++
            if (mMode == 4) {
                mMode = 1
            }
            if (mMode == 1) {
                binding.calendarView.setAllMode()
            } else if (mMode == 2) {
                binding.calendarView.setFixMode()
            } else if (mMode == 3) {
                binding.calendarView.setOnlyCurrentMode()
            }
        }
        binding.btnModeReduce.setOnClickListener {
            mMode--
            if (mMode == 0) {
                mMode = 3
            }
            if (mMode == 1) {
                binding.calendarView.setAllMode()
            } else if (mMode == 2) {
                binding.calendarView.setFixMode()
            } else if (mMode == 3) {
                binding.calendarView.setOnlyCurrentMode()
            }
        }
        binding.btnDay.setOnClickListener {
            binding.calendarView.scrollToCalendar(2023, 2, 28)
        }

        binding.btnCurrentTime.setOnClickListener {
            binding.calendarView.setCurrentTime(1675068494000, TimeZone.getTimeZone("GMT+8"))
            binding.calendarView.scrollToCurrent()
        }
    }
}